import util.TreeNode;

/**
 * Created by loso on 2017/3/17.
 */
public class _112_PathSum {



    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null){
            return false;
        }
        return findSum(root,0,sum);
    }

    public boolean findSum(TreeNode root, int sum , int target){
        if (root.left == null && root.right == null){
            if (sum+root.val == target){
                return true;
            }else{
                return false;
            }
        }
        if (root.right == null){
            return findSum(root.left,sum+root.val,target);
        }
        else if (root.left == null){
            return findSum(root.right,sum+root.val,target);
        }
        return findSum(root.left,sum+root.val,target) || findSum(root.right,sum+root.val,target);
    }

    //下一个用sum-val来判断
    public boolean hasPathSumBigboss(TreeNode root, int sum) {
        if(root == null) return false;

        if(root.left == null && root.right == null && sum - root.val == 0) return true;

        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}
